<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$merchid=$this->checklogin();
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_tihuo',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$item){
		$item['pcd']=json_decode($item['pcd'],true);
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('0'=>'禁用','1'=>'启用');
	$return['isdefault']=array('0'=>'否','1'=>'是');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'提货点管理'),
			array('op'=>'edit','title'=>'添加提货点'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'标题','type'=>'string'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'status','title'=>'是否显示','type'=>'radio','options'=>'status'),	
				
		)),
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$return['forward_url']=$_SESSION['forward_url'];
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=$this->get('tihuo',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['pcd']=json_decode($item['pcd'],true);
		$item['lat_lng']=json_decode($item['lat_lng'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'提货点设置',
		'edit'=>array(
			array('name'=>'title','title'=>'提货点名称','type'=>'string'),
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
			array('name'=>'image','title'=>'LOGO','type'=>'image'),
			array('name'=>'pcd','title'=>'区域','type'=>'pcd'),
			array('name'=>'address','title'=>'地址','type'=>'string'),
			array('name'=>'lat_lng','title'=>'坐标','type'=>'lat_lng'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'image'=>trim($postdata['image']),
		'name'=>trim($postdata['name']),
		'mobile'=>trim($postdata['mobile']),
		'pcd'=>ijson_encode($postdata['pcd']),
		'address'=>trim($postdata['address']),
		'lat_lng'=>ijson_encode($postdata['lat_lng']),
		'status'=>intval($postdata['status']),
	);
	if(!$data['title']){
		exi('请填写标题','error');
	}
	if(!$data['address']){
		exi('请填写地址','error');
	}
	$id=intval($postdata['id']);
	if($id){
		$this->update('tihuo',$data,array('id'=>$id));
		exi('设置成功！','','list');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['createtime']=time();
		$this->insert('tihuo',$data);
		exi('添加成功！','','list');
	}
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要设置的地址','error');
	}
	pdo_update('jy_merch_tihuo',array('isdefault'=>0),array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
	pdo_update('jy_merch_tihuo',array('isdefault'=>1),array('id'=>$id,'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']));
	if($_RQ['get']==1){
		exi('success');
	}else{
		exi('','','jy_merch/manage.tihuo/');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_merch_tihuo',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_merch_tihuo',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>